<?php
    /**
     * xDataBase
     *
     * The xDataBase class provides connection and using data base
     *
     * @copyright Copyright (c) 2011, xEngine Progect
     */
     
    /**
     * xDataBase
     *
     * The xDataBase class provides connection and using data base
     *
     * @copyright Copyridfdfd, xEngine Progect
     */
    class iJAdmin {
        private $tockens;
        /**
         *
         * This function is Block construct
         *
         * @access public
         */
         
        public function __construct() {
          //TODO constructor
          //TODO add top up controll
        }
        
        /**
         *
         * This function returns true value if administrator session is started
         *
         * @access static
         */
        public static function isAdminLogin() {
          if(!empty($_SESSION['userType']) && $_SESSION['userType'] == 'admin') {
            return true;
          } else {
            return false;
          }
        }
        
        /**
         *
         * Return administrator's list
         * 
         * @param int    $page   - page number
         * @param string $search - search string 
         * @param int    $limt   - result limit
         * 
         * @return administrator's list
         * @access public
         */
        public function getAdministratorsList($page, $search, $limit = '') {
          $iJDataBase  =  iJFactory::singleton('iJDataBase');
          $QAdmins    =  $iJDataBase->query('SELECT 
                                              id,
                                              login, 
                                              email,
                                              fullName,
                                              access 
                                            FROM :table_admins
                                            ');
          $QAdmins->bindTable(':table_admins', TABLE_ADMINS);
          
          if(!empty($search)) {
            $QAdmins->appendQuery('WHERE login like :search or fullName like :search');
            $QAdmins->bindValue(':search', '%' . $search . '%');
          }
          
          $QAdmins->appendQuery(':limit');
          $QAdmins->bindTable(':limit', $QAdmins->addLimit($page, $limit));
          $QAdmins->execute();
          $list = array();
          
          while($QAdmins->next()) {
            $admin['id']       = $QAdmins->value('id');
            $admin['login']    = $QAdmins->value('login');
            $admin['email']    = $QAdmins->value('email');
            $admin['access']   = $QAdmins->value('access');
            $admin['fullName'] = $QAdmins->value('fullName');
            $admin['trash']  =   iJHrefLink::linkToLocalPage('admin_control') . '?delID=' . $QAdmins->value('id');
            $list[] = $admin;
          }
          return array($list, $QAdmins);
          
        }
        
    /**
     *
     * provides administrator log in
     * 
     * @param string $login   - admininistrator login
     * @param string $pass    - admininistrator pass
     * 
     * @return true is succsess and false if fale
     * @access public
     */
    public function loginRequest($login, $pass) {
      $iJDataBase =  iJFactory::singleton('iJDataBase');
      $QAdmin = $iJDataBase->query('SELECT 
                                     login,
                                     email,
                                     fullName,
                                     access
                                   FROM :table_admins
                                     WHERE login = :login AND pass = :pass');
                                  
      $QAdmin->bindTable(':table_admins', TABLE_ADMINS);
      
      $QAdmin->bindValue(':login', $login);
      $QAdmin->bindValue(':pass',  md5($pass));
      $QAdmin->execute();
      if($QAdmin->next()) {
        $_SESSION['userType']   = 'admin';
        $_SESSION['userData']   = $QAdmin->toArray();
        return true;
        
      } else {
        return false;
      }
    }
        
    /**
     *
     * provides new administrator registration
     * 
     * @param array $adminData   - admininistrator data
     * 
     * @return true is succsess and false if fale
     * @access public
     */
    public function register($adminData) {
      //TODO add mail validation
      if (!iJValidation::name($adminData['login'])){
         iJMessages::display('fail', 'fail', 'incorrect_login');
         return;
      }    
      if (!iJValidation::name($adminData['pass'])){
         iJMessages::display('fail', 'fail', 'incorrect_pass');
         return;
      }   
      if (!iJValidation::name($adminData['re_enter'])){
         iJMessages::display('fail', 'fail', 'incorrect_pass');
         return;
      }   
      /*
      if (!xValidation::mail($blockInfo['email'])){
         xMessages::display('fail', 'fail', 'incorrect_email');
         return;
      } 
        */
      if (!iJValidation::Unexsymb($adminData['full_name'], 2)){
         iJMessages::display('fail', 'fail', 'incorrect_name');
         return;
      }  
      
      $iJDataBase  =  iJFactory::singleton('iJDataBase');
      if($adminData['pass'] != $adminData['re_enter']) {
        iJMessages::display('fail', 'fail', 'pass_mismatch');
        return;
      }
      $QAdmin    =  $iJDataBase->query('INSERT INTO :table_admins
                                                    (
                                                      login,
                                                      pass,
                                                      email,
                                                      fullName,
                                                      access
                                                    )
                                                    VALUES
                                                    (
                                                      :login,
                                                      :pass,
                                                      :email,
                                                      :fullName,
                                                      :access
                                                    )

                                        ');
      $QAdmin->bindTable(':table_admins',       TABLE_ADMINS);
      $QAdmin->bindValue(':login',              $adminData['login']);
      $QAdmin->bindValue(':pass',               md5($adminData['pass']));
      $QAdmin->bindValue(':fullName',           $adminData['full_name']);
      $QAdmin->bindValue(':email',              $adminData['email']);
      $QAdmin->bindValue(':access',             'FULL');
      $QAdmin->execute();
      if(!$iJDataBase->isError()) {
        iJMessages::display('success', 'success', 'new_admin_rigistred');
        return true;
      } else {
        iJMessages::display('fail', 'fail', 'database_error');
        return false;
      }
    } 
    
    
    /**
     *
     * provides editing administrator data
     * 
     * @param array $adminData   - new admininistrator data
     * 
     * @return true is succsess and false if fale
     * @access public
     */
    public function edit($adminData) {
      if (!iJValidation::number($adminData['id'])){
         iJMessages::display('fail', 'fail', 'wrong_id');
         return;
      }  
      if (!iJValidation::name($adminData['login'])){
         iJMessages::display('fail', 'fail', 'incorrect_login');
         return;
      }   
      
      
      if (!iJValidation::name($adminData['new_pass'], 0)){
         iJMessages::display('fail', 'fail', 'incorrect_pass');
         return;
      }   
      //TODO add mail validation
      /*
      if (!iJValidation::mail($adminData['email'])){
         iJMessages::display('fail', 'fail', 'incorrect_email');
         return;
      } */
        
      if (!iJValidation::Unex_symb($adminData['full_name'], 2)){
         iJMessages::display('fail', 'fail', 'incorrect_name');
         return;
      }  
      
      $iJDataBase  =  iJFactory::singleton('iJDataBase');
      
      $QAdmin    =  $iJDataBase->query('UPDATE  :table_admins 
                                          SET  login    = :login,
                                               email    = :email,
                                               fullName = :fullName,
                                               access   = :access

                                        ');
      $QAdmin->bindTable(':table_admins',       TABLE_ADMINS);
      $QAdmin->bindValue(':login',              $adminData['login']);
      $QAdmin->bindValue(':fullName',           $adminData['full_name']);
      $QAdmin->bindValue(':email',              $adminData['email']);
      $QAdmin->bindValue(':access',             'FULL');
      if(!empty($adminData['new_pass'])) {
        $QAdmin->appendQuery(', pass = :pass');
        $QAdmin->bindValue(':pass',               md5($adminData['new_pass']));
      }
      
      $QAdmin->appendQuery('WHERE id = :id');
      $QAdmin->bindValue(':id',              $adminData['id']);
      
      $QAdmin->execute();
      if(!$iJDataBase->isError()) {
        iJMessages::display('success', 'success', 'administrator data changed');
        return true;
      } else {
        iJMessages::display('fail', 'fail', 'database_error');
        return false;
      }
    } 
    
    
    /**
     *
     * provides removing administrator
     * 
     * @param int $adminId   - administrator id
     * 
     * @return true is succsess and false if fale
     * @access public
     */
    public function removeAdmin($adminId) {
      $iJDataBase  =  iJFactory::singleton('iJDataBase');
      if(empty($adminId)) {
        iJMessages::display('fail', 'fail', 'empty_id');
        return;
      }
      
      $QAdmin    =  $iJDataBase->query('DELETE FROM :table_admins
                                         WHERE id = :id');
                                         
      $QAdmin->bindTable(':table_admins',   TABLE_ADMINS);
      $QAdmin->bindValue(':id',             $adminId);
      $QAdmin->execute();
      if(!$iJDataBase->isError()) {
        iJMessages::display('success', 'success', 'admin_removed');
        return true;
      } else {
        iJMessages::display('fail', 'fail', 'database_error');
        return false;
      }
    }
    /**
     *
     * register admin info
     * 
     * @param int $adminId   - administrator id
     * 
     * @access public
     */
     public function registerAdminInfo($adminId) {
      $iJDataBase  =  iJFactory::singleton('iJDataBase');
      $QAdmins    =  $iJDataBase->query('SELECT 
                                            login, 
                                            email,
                                            fullName,
                                            access 
                                            FROM :table_admins
                                            WHERE id = :id
                                            ');                                      
      $QAdmins->bindTable(':table_admins', TABLE_ADMINS);
      $QAdmins->bindTable(':id', $adminId);
      $QAdmins->execute();
      if($QAdmins->next()) {
        $_SESSION['userType']             = 'admin';
        $_SESSION['userInfo']['login']    = $QAdmins->value('login');
        $_SESSION['userInfo']['email']    = $QAdmins->value('email');
        $_SESSION['userInfo']['fullName'] = $QAdmins->value('fullName');
        $_SESSION['userInfo']['access']   = $QAdmins->value('access');  
      }
      
     }
        
 
    }
?>